﻿
using Levfier.Proccessors.Parameters;
using System;
namespace Levfier.Proccessors.Filters
{
    public class PeakIIR : ShelfIIR
    {
        /// <summary>
        /// 係数を初期化する
        /// </summary>
        /// <param name="sender">イベント発生元</param>
        protected override void InitFilterCoefficient(object sender)
        {
            //ゲイン
            double gain = ProccessParameter.ConvertDBToValue(this.gainParam.Value);

            //デジタル周波数 -> アナログ周波数に変換
            double analogFreq = (Math.Tan(Math.PI * this.cutOffFrequencyParam.Value / VSTParameterManager.GetInstance().SampleRate) / (2.0d * Math.PI));

            //共通の値を計算
            double tempCommon = (2.0d * Math.PI * analogFreq);
            double tempCommon2 = tempCommon * tempCommon;
            double tempCommonMiddle = tempCommon / this.qualityFactorParam.Value;

            //係数計算
            this.b0 = (1.0d + gain * tempCommonMiddle + tempCommon2) / (1.0d + tempCommonMiddle + tempCommon2);
            this.b1 = (2.0d * tempCommon2 - 2.0d) / (1.0d + tempCommonMiddle + tempCommon2);
            this.b2 = (1.0d - gain * tempCommonMiddle + tempCommon2) / (1.0d + tempCommonMiddle + tempCommon2);
            this.a1 = (2.0d * tempCommon2 - 2.0d) / (1.0d + tempCommonMiddle + tempCommon2);
            this.a2 = (1.0d - tempCommonMiddle + tempCommon2) / (1.0d + tempCommonMiddle + tempCommon2);
        }
    }
}
